"""
This file keep syncing local directory and Google Storage.
"""
import os,sys
import shutil
import operations
from env import *


def set_local_dir(path):
  if os.path.exists(path):
    if os.path.isdir(path):
      local_dir = path
    else:
      return False
  else:
    os.makedirs(path)
    local_dir = path

def local_dir_available():
  if os.path.isdir(local_dir):
    return True 
  else:
    return False

def add_local_file(src_file, dst_file):
  """
  This function copy the source file/directory to the local_dir.
  @dst_file: relative path
  @example
    src_file: /tmp/dir/a.txt
    dst_file: a.txt
    -----------------
    src_file: /tmp/dir/
    dst_file: dir
  """
  
  if local_dir_available():
    dst_fullname = os.path.join(local_dir, dst_file)

    if os.path.isdir(src_file):
      shutil.copytree(src_file, dst_fullname)
    elif os.path.isfile(src_file):
      shutil.copyfile(src_file, dst_fullname)
  else:
    return False
  
def move_local_file(src_file, dst_file):
  """
  This function copy the src_file in local_dir to a given place.
  @example
    src_file: dir/a.txt
    dst_file: /tmp/a.txt
    -----------------
    src_file: dir
    dst_file: /tmp/dir
  """
  src_fullpath = os.path.join(local_dir, src_file)

  if os.path.isdir(src_fullpath):
    shutil.copytree(src_fullpath, dst_file)
  elif os.path.isfile(src_fullpath):
    shutil.copyfile(src_fullpath, dst_file)

def del_local_file(src_file):
  """
  @example
    src_file: dir/a.txt 
    -----------------
    src_file: dir
  """
  fullpath = os.path.join(local_dir, src_file)
  if os.path.isdir(fullpath):
    shutil.rmtree(fullpath)
  elif os.path.isfile(fullpath):
    os.remove(fullpath)

def sync():
  pass

def add_remote_file(src_file):
  """
  This function upload local file/directory to remote cloud.
  @example
    src_file: /tmp/dir/a.txt
    -----------------
    src_file: /tmp/dir/
  """
  if os.path.isfile(src_file):
    operations.upload_file(src_file, os.path.basename(src_file))
  elif os.path.isdir(src_file):
    dir_prefix = os.path.dirname(os.path.abspath(src_file))
    for root, dirs, files in os.walk(src_file, topdown=False):
      for name in files:
        full_path = os.path.abspath(os.path.join(root, name))
        rel_file = full_path[len(dir_prefix)+1:]
        print ">>> Uploading "+rel_file
        operations.upload_file(full_path, rel_file)
        operations.set_access_permission(rel_file, email_address, "FULL_CONTROL")

def rm_remote_file():
  pass

def ls_remote_file(email_address):
  return operations.ls_remote_file(email_address)

def check_file_status():
  """
  This call would sync with the GS server to see if the given file is up to date.
  According to md5 check & time comparing.
  @status: up-to-date/local-new/remote-new/uploading
  """
  pass

if __name__ == '__main__':
  if sys.argv[1] == 'upload':
    file_names = sys.argv[2:]
    for fullname in file_names:
      add_remote_file(fullname)

  elif sys.argv[1] == 'ls':
    print ls_remote_file(email_address)
